<template>
  <div>
    <pannel />

    <el-card shadow="never" class="border-0">
      <Search @search="getData" @reset="resetSearchForm" :model="searchForm">
        <search-item label="时间选择" prop="type">
          <el-radio-group v-model="searchForm.type">
            <el-radio-button label="all">全部</el-radio-button>
            <el-radio-button label="today">今天</el-radio-button>
            <el-radio-button label="yesterday">昨天</el-radio-button>
            <el-radio-button label="last7days">最近7天</el-radio-button>
          </el-radio-group>
        </search-item>

        <template #show>
          <search-item label="开始时间" prop="starttime">
            <el-date-picker v-model="searchForm.starttime" type="date" size="small" placeholder="开始时间" 
              style="width: 90%" value-format="YYYY-MM-DD" />
          </search-item>

          <search-item label="结束时间" prop="endtime">
            <el-date-picker v-model="searchForm.endtime" type="date" size="small" placeholder="结束时间" 
              style="width: 90%" value-format="YYYY-MM-DD" />
          </search-item>

          <search-item label="关键词" prop="keyword">
            <el-input v-model="searchForm.keyword" placeholder="关键词" size="small" />
          </search-item>
        </template>
      </Search>

      <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
        <el-table-column prop="id" label="ID" align="center" />
        <el-table-column label="会员" width="65">
          <template #default="{ row }">
            <div class="flex items-center">
              <el-avatar :size="40" :src="row.avatar" @error="() => true">
                <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"/>
              </el-avatar>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="用户信息" width="280">
          <template #default="{ row }">
            <div class="text-xs flex flex-col items-start">
              <p>昵称：{{ row.nickname }}</p>
              <p class="font-medium">姓名：{{ row.real_name }}</p>
              <p>电话：{{ row.phone }}</p>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="推广用户数量" prop="spread_count" />
        <el-table-column label="订单数量" prop="order_count" />
        <el-table-column label="订单金额" prop="order_price" />
        <el-table-column label="账户佣金" prop="now_money" />
        <el-table-column label="已提现金额" prop="extract_count_price" />
        <el-table-column label="提现次数" prop="extract_count_num" />
        <el-table-column label="未提现金额" prop="new_money" />
        <el-table-column fixed="right" label="操作" width="180" align="center">
          <template #default="{ row }">
            <el-button type="primary" size="small" text @click="openDataDrawer(row.id, 'user')">推广人</el-button>
            <el-button type="primary" size="small" text @click="openDataDrawer(row.id, 'order')">推广订单</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="flex items-center justify-center mt-5">
        <el-pagination background layout="prev, pager, next" :total="total" :current-page="currentPage"
          :page-size="limit" @current-change="getData"/>
      </div>
    </el-card>

    <dataDrawer ref="dataDrawerRef"/>
    <dataDrawer ref="orderDataDrawerRef" type="order"/>
  </div>
</template>

<script setup>
  import { ref } from 'vue'
  import pannel from './components/pannel.vue'
  import { getAgentListApi } from '@/api/distribution.js'
  import Search from '@/components/Search.vue'
  import SearchItem from '@/components/SearchItem.vue'
  import { useInitTable } from '@/hooks/useCommon.js'
  import dataDrawer from './components/dataDrawer.vue'

  const {
    searchForm,
    resetSearchForm,
    tableData,
    loading,
    currentPage,
    total,
    limit,
    getData,
  } = useInitTable({
    // 搜索需要的参数
    searchForm: {
      keyword: '',
      type: 'all',
      starttime: null,
      endtime: null
    },
    // 获取表单的api
    getlist: getAgentListApi,
    // 获取表单成功的回调
    onGetListSuccess: (res) => {
      tableData.value = res.list
      total.value = res.totalCount
    },
  })

  const dataDrawerRef = ref(null)
  const orderDataDrawerRef = ref(null)
  function openDataDrawer(id, type) {
    (type === 'user' ? dataDrawerRef: orderDataDrawerRef).value.open(id)
  }

</script>

<style scoped></style>
